const app = getApp()

import { HiddenCall } from '../request/api.js'
const request = new HiddenCall
import { throttle } from "../utils/throttle.js"
module.exports = {
  /**
 *  房客源筛选
 * - @author           wl
 * - @date             2018-11-10
 */
  data: {
    detailTop: false,
    isAdd: false,
    isRef: false,
    isShowTab: false,
    filterData: {},
    searchData: {}
  },
  /*
  * 关闭筛选
   */
  clickMask() {
    this.setData({ filterbar: false })
  },
  /**
   * 获取一屏的高度
   * author wl
   */
  getHeight(scenes) {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        // 获取屏幕的高
        that.setData({
          screenHeight: res.windowHeight - 180
        })
      }
    })
    // 如果没有值，不进行区域获取
    if (!scenes) {
      // this.getRegionList()
    }
  },
  /**
   * 获取滚动条的高度
   * author wl
   */
  onPageScroll(event) {
    this.setData({ scrollBarTop: event.scrollTop })
    // 房客源详情
    if (event.scrollTop >= this.data.headerHeight) {
      if (this.data.detailTop) {
        return
      }
      this.setData({ detailTop: true })
    } else {
      this.setData({ detailTop: false })
    }

    if (event.scrollTop >= this.data.headerTop) {
      if (this.data.isAdd) {
        return
      }
      this.setData({ isAdd: true })
    } else {
      this.setData({ isAdd: false })
    }
  },
  /**
   * 返回顶部
   * author wl
   */
  backTop() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 500
    })
  },
  /**
   * 获取区域列表
   * author wl
   */
  getRegionList: throttle(function() {
    // 调用接口获取区域列表
    request.regionList().then(res => {
      // 数据更新
      this.setData({
        regionList: res.result
      })
    })
  }, 2000),
  /**
  * 获取面积||户型||价格||楼层最小值
  * author wl
  */
  inputMini(e) {
    // 拿到最小值对象中的key
    let key = this.data.small[e.detail.label]
    // 给当前的key赋值
    this.setData({
      [key]: e.detail.value
    })
  },
  /**
   * 获取面积||户型||价格||楼层最大值
   * author wl
   */
  inputMaxi(e) {
    // 拿到最大值对象中的key
    let key = this.data.big[e.detail.label]
    // 给当前的key赋值
    this.setData({
      [key]: e.detail.value
    })
  },
  /**
   * 获取房源筛选拨打||面积录入||推送开始时间
   * 获取客源筛选录入||推送||最后跟进开始时间
   * author wl
   */
  startTime(e) {
    // 拿到初始时间对象中的key
    let key = this.data.start[e.detail.label]
    // 给当前的key赋值
    this.setData({
      [key]: e.detail.value
    })
  },
  /**
   * 获取房源筛选拨打||面积录入||推送结束时间
   * 获取客源筛选录入||推送||最后跟进结束时间
   * author wl
   */
  endTime(e) {
    // 拿到结束时间对象中的key
    let key = this.data.end[e.detail.label]
    // 给当前的key赋值
    this.setData({
      [key]: e.detail.value
    })
  },
   /**
    * 点击重置将相关数据中checked设置为false
    * author wl
    */
  changeBoolean() {
    // 区域布尔更改,户型布尔更改
    let arr = [this.data.regionList, this.data.houseType]
    // 循环重置
    this.forChange(arr)
    // 状态布尔更改
    if (this.data.housePublicType && this.data.housePublicType.length > 0) {
      // 循环状态赋予false
      this.data.housePublicType.forEach((el) => {
        el.checked = false
      })
      return
    }
    // 客源状态更改
    if (this.data.customerPublicType && this.data.customerPublicType.length > 0) {
       // 循环状态赋予false
      this.data.customerPublicType.forEach((el) => {
        el.checked = false
      })
      return
    }
  },
  /**
   * 点击重置，循环更改布尔为false
   * author wl
   */
  forChange(arr) {
    for (let i = 0; i < arr.length; i++) {
      // 有值的情况下
      if (arr[i] && arr[i].length > 0) {
        // 每个属性赋予false
        arr[i].forEach((el) => {
          el.checked = false
        })
      }
    }
  },
  /**
   * 重置房客源筛选相关数据
   * author wl
   */
  resetTenant() {
    // 重置搜索前数据
    this.forReset(this.data.searchData, 'searchData')
    // 重置搜索后数据
    this.forReset(this.data.filterData, 'filterData')
    // 暂存数据
    this.forReset(this.searchData)
    // 清除筛选最小值
    this.resetString(this.data.small)
    // 清除筛选最大值
    this.resetString(this.data.big)
    // 清除筛选初始时间
    this.resetString(this.data.start)
    // 清除筛选结束时间
    this.resetString(this.data.end)
  },  
  /**
   * 重置相关对象属性
   * author wl
   */
  forReset (data, key) {
    // 属性赋值
    for (let i in data) {
      data[i] = ""
    }
    // 有key的情况下赋值更新
    if (key && data) {
      this.setData({
        [key]: data
      })
    }
  },
  /**
   * 重置相关字符串类型数据
   * author wl
   */
  resetString (str) {
    for (let i in str) {
      this.setData({
        [str[i]]: ""
      })
    }
  },
  /**
 * 渲染房客源区域高亮
 * author wl
 */
  forArrRendering(data, arr) {
    // 循环列表数据
    for (let j = 0; j < data.length; j++) {
      // 循环选中的数据
      for (let r = 0; r < arr.length; r++) {
        // 全部赋值为false
        data[j].checked = false
        // 对比相同赋值为true
        if (arr[r] === data[j].value || arr[r] === data[j].code) {
          data[j].checked = true
          break
        }
      }
    }
  },
  /**
   * 渲染房客源状态
   * author wl
   * prop           对象中的key
   * data            全部筛选数据
   * jsonData     筛选条件数据
   * i                   索引
   * key              房源状态还是客源状态
   */
  renderingStatus(prop, data, jsonData, i, key) {
    // 状态
    if (prop === 'state') {
      // 对比值相同true
      if (data[prop][i].value === jsonData[prop]) {
        data[prop][i].checked = true
      } else {
        // 不同则赋值为false
        data[prop][i].checked = false
      }
      // 数据更新
      this.setData({
        [key]: this.data[key]
      })
    }
  },
  /**
 * 将筛选的数据整理放入到搜索数据model进行搜索
 * author wl
 * filter       筛选条件数据
 * hide       进行筛选搜索时
 * give       搜素数据model
 */
  searchPool(filter, give, hide) {
    // 循环赋值筛选数据
    for (let key in filter) {
      if (filter[key]) {
        give[key] = filter[key]
      }
    }
    // 点击确定筛选关闭选项菜单
    hide ? this.clickMask() : ""
  },
  /**
   * 修改已选中的筛选条件
   * author wl
   */
  modifyFilter(confirm, file, hide, resourceType) {
    if (app.globalData.filterData && Object.keys(app.globalData.filterData).length > 0) {
      this.pageFor()
    }
    // 是否选中了筛选条件
    if (file) {
      // 循环数据赋值
      for (let i in this.searchData.jsonData) {
        if (this.searchData.jsonData[i]) {
          // 将选泽要修改的舒服赋予file
          file.jsonData[i] = this.searchData.jsonData[i]
        }
      }
      // 修改筛选名字的话赋予
      this.searchData.screeningName ? file.screeningName = this.searchData.screeningName : ""
      if (confirm) {
        // 修改接口调用
        request[resourceType === "house" ? 'houseFilterModify' : 'customerFilterModify'](file).then((res) => { })
      } else {
        // 确定筛选条件，循环筛选条件赋值搜索model
        this.searchPool(file.jsonData, this.data.filterData, hide)
      }
    } else {
      // 没有选中条件的时候，循环筛选条件赋值搜索model，确定筛选条件
      this.searchPool(this.searchData.jsonData, this.data.filterData, hide)
    }

    // 搜素条件更新
    app.globalData.filterData = this.data.filterData
    this.setData({ filterData: this.data.filterData })
    // 调用搜索房源
    if (resourceType ==="house") {
      if (hide) {
        app.globalData.houseFilter.houseSearch()
        wx.navigateBack({
          delta: 1
        })
      }
    } else {
      // 调用搜索客源
      if (hide) {
        app.globalData.customerFilter.customerSearch()
        wx.navigateBack({
          delta: 1
        })
      }
    }
  },
  // 循环赋值
  pageFor (data) {
    for (let i in this.searchData.jsonData) {
      this.searchData.jsonData[i] = app.globalData.filterData[i]
    }
  },
  /**
   * 渲染字符串类型数据，时间类型去除时分秒，以免数据不对应
   * author wl
   */
  clearHour(jsonData, prop, data) {
    // 渲染不是数组类型
    if (jsonData[prop] && !(data[prop] instanceof Array)) {
      // 时间类型赋值
      if (prop === 'followupTimeStart' || prop === 'followupTimeEnd' || prop === 'entryTimeStart' || prop === 'entryTimeEnd' || prop === 'createtimeStart' || prop === 'createtimeEnd' || prop === 'lastCallTimeStart' || prop === 'lastCallTimeEnd') {
        // 渲染的时候将时分秒替换为空
        this.data.searchData[prop] = jsonData[prop].replace(/\s+[0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/, "")
      } else {
        // 不是时间类型直接赋值
        this.data.searchData[prop] = jsonData[prop]
      }
    }
  },
  /**
 * 房源筛选数据汇集
 * author wl
 * @param  utype  房客源类型
 * @param  state   状态
 * @param favourite  收藏夹
 * @param region    区域
 * @param houseDescribe 户型
 * @param houseAreaStart 面积开始
 * @param houseAreaEnd 面积结束
 * @param housePriceStart 租售价开始
 * @param housePriceEnd 租售价结束
 * @param houseFloorStart 楼层开始
 * @param houseFloorEnd 楼层结束
 * @param lastCallTimeStart 拨打时间开始
 * @param lastCallTimeEnd 拨打时间结束
 * @param entryTimeStart 面积录入时间开始
 * @param entryTimeEnd 面积录入时间结束
 * @param createtimeStart 推送时间开始
 * @param createtimeEnd 推送时间结束
 */
  houseFilterModel(e) {
    this.searchData = {
      "jsonData": {
        "utype": e.detail.utype === "买卖" ? "出售" : e.detail.utype === "租赁" ? "出租" : "",
        "state": this.data.state,
        "region": this.data.regionValue,
        "favourite": e.detail.favourite,
        "houseDescribe": this.data.houseValue,
        "houseAreaStart": this.data.houseAreaStart,
        "houseAreaEnd": this.data.houseAreaEnd,
        "housePriceStart": this.data.housePriceStart,
        "housePriceEnd": this.data.housePriceEnd,
        "houseFloorStart": this.data.houseFloorStart,
        "houseFloorEnd": this.data.houseFloorEnd,
        "lastCallTimeStart": this.data.lastCallTimeStart ? `${this.data.lastCallTimeStart} 00:00:00` : "",
        "lastCallTimeEnd": this.data.lastCallTimeEnd ? `${this.data.lastCallTimeEnd} 23:59:59` : "",
        "entryTimeStart": this.data.entryTimeStart ? `${this.data.entryTimeStart} 00:00:00` : "",
        "entryTimeEnd": this.data.entryTimeEnd ? `${this.data.entryTimeEnd} 23:59:59` : "",
        "createtimeStart": this.data.createtimeStart ? `${this.data.createtimeStart} 00:00:00` : "",
        "createtimeEnd": this.data.createtimeEnd ? `${this.data.createtimeEnd} 23:59:59` : ""
      },
      "screeningName": e.detail.fileName
    }
  },
  /**
   * 选择房源筛选条件渲染
   * author wl
   */
  houseFilterRender(jsonData) {
    let data = {
      "state": this.data.housePublicType,
      "region": this.data.regionList,
      "favourite": jsonData.favourite,
      "houseDescribe": this.data.houseType,
      "houseAreaStart": jsonData.houseAreaStart,
      "houseAreaEnd": jsonData.houseAreaEnd,
      "housePriceStart": jsonData.housePriceStart,
      "housePriceEnd": jsonData.housePriceEnd,
      "houseFloorStart": jsonData.houseFloorStart,
      "houseFloorEnd": jsonData.houseFloorEnd,
      "lastCallTimeStart": jsonData.lastCallTimeStart,
      "lastCallTimeEnd": jsonData.lastCallTimeEnd,
      "entryTimeStart": jsonData.entryTimeStart,
      "entryTimeEnd": jsonData.entryTimeEnd,
      "createtimeStart": jsonData.createtimeStart,
      "createtimeEnd": jsonData.createtimeEnd
    }
    // 渲染数据
    this.houseForRendering(jsonData, data)
  },
  /**
   * 客源汇集筛选数据
   * author wl
   * @param  utype  房客源类型
   * @param  state   状态
   * @param favourite  收藏夹
   * @param region    需求区域
   * @param customerAreaStart 需求面积开始
   * @param customerAreaEnd 需求面积结束
   * @param customerPriceStart 需求价格开始
   * @param customerPriceEnd 需求价格结束
   * @param entryTimeStart 录入时间开始
   * @param entryTimeEnd 录入时间结束
   * @param createtimeStart 推送时间开始
   * @param createtimeEnd 推送时间结束
   * @param followupTimeStart 最后跟进时间开始
   * @param followupTimeEnd 最后跟进时间结束
   */
  customerFilterModel (e) {
    this.searchData = {
      "jsonData": {
        "utype": e.detail.utype === "买卖" ? "求购" : e.detail.utype === "租赁" ? "求租" : "",
        "state": this.data.state,
        "customerRegion": this.data.regionValue,
        "favourite": e.detail.favourite,
        "customerDescribeStart": this.data.customerDescribeStart,
        "customerDescribeEnd": this.data.customerDescribeEnd,
        "customerFloorEnd": this.data.customerFloorEnd,
        "customerAreaStart": this.data.customerAreaStart,
        "customerAreaEnd": this.data.customerAreaEnd,
        "customerPriceStart": this.data.customerPriceStart,
        "customerPriceEnd": this.data.customerPriceEnd,
        "entryTimeStart": this.data.entryTimeStart ? `${this.data.entryTimeStart} 00:00:00` : "",
        "entryTimeEnd": this.data.entryTimeEnd ? `${this.data.entryTimeEnd} 23:59:59` : "",
        "createtimeStart": this.data.createtimeStart ? `${this.data.createtimeStart} 00:00:00` : "",
        "createtimeEnd": this.data.createtimeEnd ? `${this.data.createtimeEnd} 23:59:59` : "",
        "followupTimeStart": this.data.followupTimeStart ? `${this.data.followupTimeStart} 00:00:00` : "",
        "followupTimeEnd": this.data.followupTimeEnd ? `${this.data.followupTimeEnd} 23:59:59` : ""
      },
      "screeningName": e.detail.fileName
    }
  },
  /**
   * 选中客源筛选条件渲染
   * author wl
   */
  customerFilterRender(jsonData) {
    // 选择筛选条件的数据后赋予新的对象
    let data = {
      "state": this.data.customerPublicType,
      "customerRegion": this.data.regionList,
      "favourite": jsonData.favourite,
      "customerDescribeStart": jsonData.customerDescribeStart,
      "customerDescribeEnd ": jsonData.customerDescribeEnd,
      "customerAreaStart": jsonData.customerAreaStart,
      "customerAreaEnd": jsonData.customerAreaEnd,
      "customerPriceStart": jsonData.customerPriceStart,
      "customerPriceEnd": jsonData.customerPriceEnd,
      "entryTimeStart": jsonData.entryTimeStart,
      "entryTimeEnd": jsonData.entryTimeEnd,
      "createtimeStart": jsonData.createtimeStart,
      "createtimeEnd": jsonData.createtimeEnd,
      "followupTimeStart": jsonData.entryTimeStart,
      "followupTimeEnd": jsonData.entryTimeEnd
    }
    // 渲染数据data
    this.customerForRendering(jsonData, data)
  },
  /**
   * 所有列表筛选选择房客源数据判断
   * author wl
   */
  tenanTypeSelect() {
    // 选择了房源
    if (this.data.resourceValue === "房源") {
      // 开始的时候以买卖判断，如果被选择之后以求购判断
      this.data.saleValue = this.data.saleValue ? (this.data.saleValue === "买卖" || this.data.saleValue === "求购" ? "出售" : "出租") : ""
    } else {
      this.data.saleValue = this.data.saleValue ? (this.data.saleValue === "买卖" || this.data.saleValue === "出售" ? "求购" : "求租") : ""
    }
  },
  myCatchTouch: function () {
    return
  }
}